﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
using System.Web.Routing;
using System.Web.Mvc;
using System.Web;
using System.ComponentModel;

namespace MvcShop.Models.ViewDataModels
{
    /// <summary>
    /// MvcSiteMapNode visibility
    /// </summary>
    public enum MvcSiteMapNodeVisibility
    {
        /// <summary>
        /// Full visibility (default)
        /// </summary>
        Full = 0,

        /// <summary>
        /// Visible in SiteMapPath only, not in Menu
        /// </summary>
        InSiteMapPathOnly = 1
    }

    /// <summary>
    /// MvcSiteMapNode
    /// </summary>
    public class MvcSiteMapNode : SiteMapNode
    {
        #region Fields

        /// <summary>
        /// Url
        /// </summary>
        protected string url;

        /// <summary>
        /// Title
        /// </summary>
        protected string title;

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the target
        /// </summary>
        public string Target { get; set; }

        /// <summary>
        /// Area name the SiteMap node is linked to
        /// </summary>
        public string Area { get; set; }

        /// <summary>
        /// Controller name the SiteMap node is linked to
        /// </summary>
        public string Controller { get; set; }

        /// <summary>
        /// Action method name the SiteMap node is linked to
        /// </summary>
        public string Action { get; set; }

        /// <summary>
        /// Sets the current SiteMap node as dynamic
        /// </summary>
        public bool IsDynamic { get; set; }

        /// <summary>
        /// Parameters to ignore when IsDynamic is true 
        /// </summary>
        public List<string> DynamicParameters { get; private set; }

        /// <summary>
        /// Key of parent SiteMap node
        /// </summary>
        public string ParentKey { get; set; }

        /// <summary>
        /// Optional imageUrl
        /// </summary>
        public string ImageUrl { get; set; }

        /// <summary>
        /// MvcSiteMapNode visibility 
        /// </summary>
        public MvcSiteMapNodeVisibility Visibility { get; set; }

        /// <summary>
        /// Route values
        /// </summary>
        public IDictionary<string, object> RouteValues { get; set; }

        /// <summary>
        /// Url
        /// </summary>
        public override string Url
        {
            get
            {
                if (!string.IsNullOrEmpty(this.url))
                    return this.url;

                RequestContext ctx;
                if (HttpContext.Current.Handler is MvcHandler)
                    ctx = ((MvcHandler)HttpContext.Current.Handler).RequestContext;
                else
                    ctx = new RequestContext(new HttpContextWrapper(HttpContext.Current), new RouteData());

                return new UrlHelper(ctx).Action(Action, Controller, new RouteValueDictionary(RouteValues));
            }
            set
            {
                this.url = value;
            }
        }

        /// <summary>
        /// Title
        /// </summary>
        [Localizable(true)]
        public override string Title
        {
            get
            {
                if (this.Provider.EnableLocalization)
                {
                    string implicitResourceString = this.GetImplicitResourceString("title");
                    if (implicitResourceString != null && implicitResourceString == this.title)
                    {
                        return implicitResourceString;
                    }
                    implicitResourceString = this.GetExplicitResourceString("title", this.title, true);
                    if (implicitResourceString != null && implicitResourceString == this.title)
                    {
                        return implicitResourceString;
                    }
                }
                if (this.title != null)
                {
                    return this.title;
                }
                return string.Empty;
            }
            set
            {
                this.title = value;
            }
        }

        #endregion

        #region Constructor

        ///// <summary>
        ///// Creates a new MvcSiteMapNode instance
        ///// </summary>
        //public MvcSiteMapNode(SiteMapProvider provider, string key)
        //    : base(provider, key)
        //{
        //    IsDynamic = false;
        //    DynamicParameters = new List<string>();
        //    Visibility = MvcSiteMapNodeVisibility.Full;
        //}

        /// <summary>
        /// Creates a new MvcSiteMapNode instance
        /// </summary>
        public MvcSiteMapNode(SiteMapProvider provider, string key, NameValueCollection explicitResourceKeys)
            : base(provider, key, "", "", "", new ArrayList(),
            new NameValueCollection(), explicitResourceKeys, "")
        {
            IsDynamic = false;
            DynamicParameters = new List<string>();
            Visibility = MvcSiteMapNodeVisibility.Full;
        }

        #endregion
    }
}
